home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / TEXT_UTL / BOYMO4 / BOYERMO2.PAS < prev   
Pascal/Delphi Source File  |  1988-01-23  |  15KB  |  242 lines

  1. { BOYERMO2.PAS (23 January 1988) (Rufus S. Hendon) }
  2.  
  3. {    This unit provides facilities for searching a text for a target using
  4.   the Boyer-Moore search method.  The routine is based on Don Strenczewilk's
  5.   implementation of a variant form of the Boyer-Moore method (his case-
  6.   insensitive version B1, available on CompuServe in file BLINE.ARC in
  7.   Borland BPROGA Data Library 4, uploaded 21 August 1987).  In addition to
  8.   repackaging his routine as a Turbo Pascal 4.0 unit, I have modified it
  9.   (1) to provide protection against endless loops that in the original
  10.   version can arise due to wrap-around of the index used to scan the text
  11.   when the the length of the text approaches the maximum (65521 characters)
  12.   allowed by Turbo Pascal 4.0 for arrays of type CHAR and (2) to improve
  13.   efficiency slightly by removing three instructions (a PUSH, a MOV, and a
  14.   POP) from the comparison loop.
  15.      The text to be searched must be stored in an array of type CHAR or an
  16.   equivalent user-defined type.  The lower bound of the array must be 1.
  17.   The target for which the text is to be searched must be of type STRING.
  18.   The program must also provide a variable for the storage of the shift
  19.   table used by the Boyer-Moore method when it searches the text.  This
  20.   variable must provide 256 bytes of storage; it can, for example, be a
  21.   variable of type ARRAY[CHAR] OF BYTE.  The target variable and the shift-
  22.   table variable must be in the same segment:  they must both be global
  23.   variables (located in the data segment) or both local variables (stored
  24.   in the stack segment).
  25.      Whenever the text is to be searched for a new target, the program must
  26.   call MAKE_BOYER_MOORE_TABLE to create the shift table for the target.
  27.   Thereafter the text can be searched for the target by invoking
  28.   BOYER_MOORE_SEARCH, specifying as arguments the target and its shift
  29.   table as well as the position in the text where the search is to begin.
  30.   If the program maintains multiple target variables and a separate shift
  31.   table and starting-position variable for each target, searches for
  32.   occurrences of the various targets can be underway simultaneously.
  33.      In a call to BOYER_MOORE_SEARCH, the argument associated with the
  34.   parameter START determines the position in the text with which the search
  35.   begins.  To search the entire text, the function would be invoked with
  36.   START = 1.  The function scans the text beginning from the START position
  37.   for the first substring that matches the target specified by the variable
  38.   associated with the parameter TARGET, using the shift table stored in the
  39.   variable associated with the parameter TABLE.  If such a substring is
  40.   found, the function returns the position (array subscript) of the initial
  41.   character of the matching substring; since the array is required to have
  42.   1 as its lower bound, the position returned after a successful search
  43.   will always be greater than 0.  If the function fails to find a matching
  44.   substring, it returns 0.  (If the requirement that the TARGET and TABLE
  45.   variables be in the same segment is violated, the function also returns
  46.   0.)
  47.      When it is required that all occurrences in the text of a given target
  48.   be found, BOYER_MOORE_SEARCH would be invoked in a loop, in which the
  49.   START argument would initially have the value of 1; thereafter, after
  50.   every successful search, the START argument would be reset to the
  51.   position returned by the function plus 1.  The loop would terminate when
  52.   the function reported failure.  The loop would have a general structure
  53.   similar to this:
  54.  
  55.     item := [the target string];
  56.     make_Boyer_Moore_table(item,shift_table);
  57.     scan_beginning := 1;
  58.     search_text_length := length(search_text);
  59.     repeat
  60.       i := Boyer_Moore_search(search_text,scan_beginning,search_text_length,
  61.           item,shift_table);
  62.       if i > 0 then begin
  63.         [do whatever processing is required when the search is successful];
  64.         scan_beginning := i+1
  65.       end
  66.     until i = 0
  67.  
  68.      Note that if the text array can only be referred to by means of a
  69.   pointer, as will be the case if the array is allocated in the heap by
  70.   means of the NEW procedure, the pointer, when used as the first argument
  71.   of BOYER_MOORE_SEARCH, must be dereferenced by writing '^' after it.  If,
  72.   for example, TEXTPTR is a pointer to the text array, the call to the
  73.   search function in the loop just given would take this form:
  74.  
  75.       i := Boyer_Moore_search(textptr^,scan_beginning,search_text_length,
  76.           item,shift_table);
  77.                                                                              }
  78. {============================================================================}
  79. unit BOYERMO2;
  80. {============================================================================}
  81. interface
  82.  
  83. procedure MAKE_BOYER_MOORE_TABLE(var target: string; var table);
  84. { TARGET is the target string for which a text is to be searched.  The
  85.   shift table for the target string is constructed in TABLE, which must be
  86.   a variable providing 256 bytes of storage, e.g. a variable declared as
  87.   ARRAY[CHAR] OF BYTE. }
  88.  
  89. function BOYER_MOORE_SEARCH(var text_array; start, text_length: word;
  90.     var target: string; var table): word;
  91. { TEXT_ARRAY is an array of characters in which a text is stored; the
  92.   text begins in TEXT_ARRAY[1] and is TEXT_LENGTH characters long.  TARGET
  93.   must either be the same variable used as parameter TARGET in an earlier
  94.   call to MAKE_BOYER_MOORE_TABLE or another variable with the same value.
  95.   TABLE must be the variable that was used as parameter TABLE in the same
  96.   call to MAKE_BOYER_MOORE_TABLE.  TARGET and TABLE must be in the same
  97.   segment, i.e. they must both be global variables or both local variables.
  98.   A Boyer-Moore search is performed on the text in TEXT_ARRAY, beginning
  99.   with the character in position START and using shift table TABLE, for
  100.   the first substring that matches TARGET.  If a match is found, the
  101.   position of the first character of the matching substring is returned.
  102.   Otherwise 0 is returned.  A function value of 0 is also returned if TABLE
  103.   and TARGET are not in the same segment. }
  104. {============================================================================}
  105. implementation
  106.  
  107. const
  108.   copy: string = '';
  109. var
  110.   table: array[char] of byte;
  111. {****************************************************************************}
  112. procedure MAKE_BOYER_MOORE_TABLE(var target: string; var table);
  113. { TARGET is the target string for which a text is to be searched.  The
  114.   shift table for the target string is constructed in TABLE, which must be
  115.   a variable providing 256 bytes of storage, e.g. a variable declared as
  116.   ARRAY[CHAR] OF BYTE. }
  117. begin { MAKE_BOYER_MOORE_TABLE }
  118.   inline
  119.     ($1E/              {       push ds            }
  120.      $C5/$76/<target/  {       lds si,[bp+target] }
  121.      $89/$F3/          {       mov bx,si          }
  122.      $8A/$04/          {       mov al, [si]       }
  123.      $88/$C4/          {       mov ah,al          }
  124.      $B9/$80/$00/      {       mov cx,$0080       }
  125.      $C4/$7E/<table/   {       les di,[bp+table]  }
  126.      $89/$FA/          {       mov dx,di          }
  127.      $FC/              {       cld                }
  128.      $F2/$AB/          {       rep stosw          }
  129.      $89/$DE/          {       mov si,bx          }
  130.      $89/$D7/          {       mov di,dx          }
  131.      $46/              {       inc si             }
  132.      $98/              {       cbw                }
  133.      $3C/$01/          {       cmp al,1           }
  134.      $7E/$13/          {       jle done           }
  135.      $48/              {       dec ax             }
  136.      $88/$E1/          {       mov cl,ah          }
  137.      $88/$E7/          {       mov bh,ah          }
  138.      $8A/$1C/          { next: mov bl,[si]        }
  139.      $89/$C2/          {       mov dx,ax          }
  140.      $29/$CA/          {       sub dx,cx          }
  141.      $88/$11/          {       mov [bx+di],dl     }
  142.      $46/              {       inc si             }
  143.      $41/              {       inc cx             }
  144.      $39/$C1/          {       cmp cx,ax          }
  145.      $75/$F2/          {       jne next           }
  146.      $1F)              { done: pop ds             }
  147. end; { MAKE_BOYER_MOORE_TABLE }
  148. {****************************************************************************}
  149. function BOYER_MOORE_SEARCH(var text_array; start, text_length: word;
  150.     var target: string; var table): word;
  151. { TEXT_ARRAY is an array of characters in which a text is stored; the
  152.   text begins in TEXT_ARRAY[1] and is TEXT_LENGTH characters long.  TARGET
  153.   must either be the same variable used as parameter TARGET in an earlier
  154.   call to MAKE_BOYER_MOORE_TABLE or another variable with the same value.
  155.   TABLE must be the variable that was used as parameter TABLE in the same
  156.   call to MAKE_BOYER_MOORE_TABLE.  TARGET and TABLE must be in the same
  157.   segment, i.e. they must both be global variables or both local variables.
  158.   A Boyer-Moore search is performed on the text in TEXT_ARRAY, beginning
  159.   with the character in position START and using shift table TABLE, for
  160.   the first substring that matches TARGET.  If a match is found, the
  161.   position of the first character of the matching substring is returned.
  162.   Otherwise 0 is returned.  A function value of 0 is also returned if TABLE
  163.   and TARGET are not in the same segment. }
  164. begin { BOYER_MOORE_SEARCH }
  165.   inline
  166.     ($1E/                  {            push ds                 }
  167.      $33/$C0/              {            xor ax,ax               }
  168.      $C5/$5E/<table/       {            lds bx,[bp+table]   } { If TABLE and  }
  169.      $8C/$D9/              {            mov cx,ds           } { TARGET are in }
  170.      $C5/$76/<target/      {            lds si,[bp+target]  } { different     }
  171.      $8C/$DA/              {            mov dx,ds           } { segments, re- }
  172.      $3B/$D1/              {            cmp dx,cx           } { port failure  }
  173.      $75/$76/              {            jne notfound2       } { at once       }
  174.      $8A/$F4/              {            mov dh,ah               }
  175.      $8A/$14/              {            mov dl,[si]             }
  176.      $80/$FA/$01/          {            cmp dl,1                }
  177.      $7F/$1F/              {            jg boyer                }
  178.      $7C/$6B/              {            jl notfound2            }
  179.      $8A/$44/$01/          {            mov al,[si+1]           }
  180.      $8B/$56/<start/       {            mov dx,[bp+start]       }
  181.      $4A/                  {            dec dx                  }
  182.      $8B/$4E/<text_length/ {            mov cx,[bp+text_length] }
  183.      $2B/$CA/              {            sub cx,dx               }
  184.      $C4/$7E/<text_array/  {            les di,[bp+text_array]  }
  185.      $8B/$DF/              {            mov bx,di               }
  186.      $03/$FA/              {            add di,dx               }
  187.      $FC/                  {            cld                     }
  188.      $F2/$AE/              {            repne scasb             }
  189.      $75/$53/              {            jne notfound2           }
  190.      $97/                  {            xchg ax,di              }
  191.      $2B/$C3/              {            sub ax,bx               }
  192.      $EB/$50/              {            jmp short exit          }
  193.      $FE/$CA/              { boyer:     dec dl                  }
  194.      $03/$F2/              {            add si,dx               }
  195.      $C4/$7E/<text_array/  {            les di,[bp+text_array]  }
  196.      $8B/$CF/              {            mov cx,di               }
  197.      $03/$4E/<text_length/ {            add cx,[bp+text_length] }
  198.      $49/                  {            dec cx                  }
  199.      $4F/                  {            dec di                  }
  200.      $03/$7E/<start/       {            add di,[bp+start]       }
  201.      $03/$FA/              {            add di,dx               }
  202.      $8A/$74/$01/          {            mov dh,[si+1]           }
  203.      $55/                  {            push bp                 }
  204.      $8B/$E9/              {            mov bp,cx               }
  205.      $8A/$EC/              {            mov ch,ah               }
  206.      $FD/                  {            std                     }
  207.      $EB/$05/              {            jmp short comp          }
  208.      $D7/                  { nexttable: xlat                    }
  209.      $03/$F8/              {            add di,ax               }
  210.      $72/$2A/              {            jc notfound             }
  211.      $3B/$EF/              { comp:      cmp bp,di               }
  212.      $72/$26/              {            jb notfound             }
  213.      $26/$8A/$05/          {            mov al,es:[di]          }
  214.      $3A/$F0/              {            cmp dh,al               }
  215.      $75/$F0/              {            jne nexttable           }
  216.      $4F/                  {            dec di                  }
  217.      $8A/$CA/              {            mov cl,dl               }
  218.      $F3/$A6/              {            repe cmpsb              }
  219.      $74/$0D/              {            je found                }
  220.      $8A/$C2/              {            mov al,dl               }
  221.      $2B/$C1/              {            sub ax,cx               }
  222.      $03/$F8/              {            add di,ax               }
  223.      $47/                  {            inc di                  }
  224.      $03/$F0/              {            add si,ax               }
  225.      $8A/$C6/              {            mov al,dh               }
  226.      $EB/$DC/              {            jmp short nexttable     }
  227.      $5D/                  { found:     pop bp                  }
  228.      $C4/$46/<text_array/  {            les ax,[bp+text_array]  }
  229.      $97/                  {            xchg ax,di              }
  230.      $2B/$C7/              {            sub ax,di               }
  231.      $40/                  {            inc ax                  }
  232.      $40/                  {            inc ax                  }
  233.      $EB/$03/              {            jmp short exit          }
  234.      $5D/                  { notfound:  pop bp                  }
  235.      $32/$C0/              { notfound2: xor al,al               }
  236.      $89/$46/$FE/          { exit:      mov [bp-2],ax           }
  237.      $FC/                  {            cld                     }
  238.      $1F)                  {            pop ds                  }
  239. end; { BOYER_MOORE_SEARCH }
  240. {****************************************************************************}
  241. end.
  242.